import React, {Component} from 'react';
import './index.less';
import moment from 'moment';
import EmptyImg from '../../../../../../hrpub/common/components/emptyImg';

class MainTable extends Component {
    constructor(props) {
        super(props);
        this.state = {
            tableHeight: ''
        };
        this.setTableHeight = this.setTableHeight.bind(this);
        this.beforeEventHandle = this.beforeEventHandle.bind(this);
        this.afterEventHandle = this.afterEventHandle.bind(this);
    }

    componentDidMount() {
        this.setTableHeight();
    }

    setTableHeight() {
        const mainTable = document.getElementsByClassName('main-table');
        if (mainTable[0]) {
            this.setState({
                tableHeight: mainTable[0].clientHeight - 36
            });
        }
    }

    beforeEventHandle(props, moduleId, item, index, value, record) {
        return true; //record.values.conttype.value === "0";
    }

    afterEventHandle(props, moduleId, key, value, changedrows, index, record) {
        if (key === 'termtype' && value === 'CM02') {
            record.values.itermmonth.value = '';
            record.values.sedenddate.value = '';
            this.props.editTable.setRowByIndex('contopiniongrid', record, index);
        }
        if (key === 'itermmonth') {
            const nextDate = this.getNextDate(
                value,
                record.values['pk_psndoc_sub.enddate'].value,
                record.values['contopinion_unit'].value
            );
            record.values.sedenddate.value = nextDate;
            this.props.editTable.setRowByIndex('contopiniongrid', record, index);
        }
    }

    getNextDate(itermmonth = 0, enddate, unit) {
        //"1":"年" "2":"月" "3": "天"
        let nextDate = '';
        if (unit === '1') {
            //nextDate = new Date(enddate).setFullYear(new Date(enddate).getFullYear() + Number(itermmonth));
            nextDate = moment(enddate).add(Number(itermmonth), 'y');
        }
        if (unit === '2') {
            //nextDate = new Date(enddate).setMonth(new Date(enddate).getMonth() + Number(itermmonth));
            nextDate = moment(enddate).add(Number(itermmonth), 'M');
        }
        if (unit === '3') {
            //nextDate = new Date(enddate).setDate(new Date(enddate).getDate() + Number(itermmonth));
            nextDate = moment(enddate).add(Number(itermmonth), 'd');
        }
        return nextDate.format('YYYY-MM-DD');
    }

    render() {
        const {editTable, orgVal, json, isEdit} = this.props;

        return (
            <div className="main-table">
                {editTable.createEditTable('contopiniongrid', {
                    cancelCustomRightMenu: true,
                    showIndex: false,
                    showCheck: !isEdit,
                    height: this.state.tableHeight,
                    onBeforeEvent: this.beforeEventHandle,
                    onAfterEvent: this.afterEventHandle
                })}
            </div>
        );
    }
}

export default MainTable;
